<template>
  <!-- 添加或修改菜单对话框 -->
  <el-dialog :title="!form.menuId ? '新增' : '修改'" :visible.sync="visible" :before-close="beforeClose">
    <el-form ref="dataForm" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col>
          <el-form-item label="上级菜单">
            <treeselect
              v-model="form.pid"
              :options="menuOptions"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级菜单"
            />
          </el-form-item>
        </el-col>
        <el-form-item label="经营范围名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入经营范围名称" />
        </el-form-item>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj
} from "@/api/scopebusiness1";
import Treeselect from "@riophae/vue-treeselect";
import TableForm from "./";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  
  name: "Menu",
  components: { Treeselect, TableForm },
  data() {
    return {
       dialogFlag:false,
      // 遮罩层
      loading: true,
      // 菜单树选项
      menuOptions: [],
      // 是否显示弹出层
      visible: false,
      form: {
        name: undefined,
        pid:0
      },
      // 表单校验
      rules: {
        name: [{ required: true, message: "经营范围名称不能为空", trigger: "blur" }],
      }
    };
  },
  methods: {
    beforeClose(done){
      if(this.dialogFlag == true){
          done()
      }
      if(this.dialogFlag == false){
          this.$confirm("可能有未保存的数据，是否关闭", '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
              }).then(data => {
              done()
          }).catch(_ => {
                  // done()
          });
      }
          
    },

    init(isEdit, id) {

      if (id != null) {
        this.form.pid = id;
      }
      this.visible = true;
      this.getTreeselect();
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (isEdit) {
          getObj(id).then(response => {
            this.form = response.data.data;
          });
        } else {
          this.form.menuId = undefined;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (this.form.pid === undefined) {
            this.form.pid = -1;
          }

          if (this.form.menuId) {
            putObj(this.form).then(data => {
              this.$message.success("修改成功");
               this.dialogFlag = true
              this.visible = false;
              this.$emit("refreshDataList");
            });
             setTimeout(()=>{
                this.dialogFlag = false
            },500)
          } else {
            addObj(this.form).then(data => {
              this.$message.success("添加成功");
               this.dialogFlag = true
              this.visible = false;
              this.$emit("refreshDataList");
            });
             setTimeout(()=>{
                this.dialogFlag = false
            },500)
          }
        }
      });
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      fetchList().then(response => {
        this.menuOptions = [];
        const menu = { id: 0, name: "根菜单", scopeBusinessList: [] };
        menu.scopeBusinessList = response.data.data;
        console.log(menu);
        this.menuOptions.push(menu);
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (
        node.scopeBusinessList &&
        !node.scopeBusinessList.length
      ) {
        delete node.scopeBusinessList;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.scopeBusinessList
      };
    }
  }
};
</script>
